package com.macro.mall.controller;

import com.alibaba.fastjson.JSON;
import com.macro.mall.bean.entity.OdrOrder;
import com.macro.mall.bean.entity.OdrOrderFlowLog;
import com.macro.mall.bean.vo.*;
import com.macro.mall.constans.PathConstants;
import com.macro.mall.service.OdrOrderService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

@Slf4j
@RestController
@RequestMapping(PathConstants.ODR_ORDRE)
@Api(value = "订单中心接口",tags = "订单中心接口")
public class OdrOrderController {

    @Autowired
    OdrOrderService odrOrderService;

    @ApiOperation(value = "订单提交接口", notes = "订单提交接口", position = 1)
    @PostMapping(value = PathConstants.ODR_ORDRE_COMMIT,consumes = MediaType.APPLICATION_JSON_VALUE,produces = MediaType.APPLICATION_JSON_VALUE)
    public RespVO commit(/*@ApiParam(value = "业务流水号", required = true, name = "x-b3-traceId") @RequestHeader("x-b3-traceId") String traceId,
                         @ApiParam(value = "请求流水号", required = true, name = "x-b3-spanId") @RequestHeader("x-b3-spanId") String spanId,*/
                         @RequestBody @ApiParam(value = "请求报文", required = true) @Valid OrderCommitReqVO request) {
        log.info("订单提交请求报文：{}",request);
        String orderId = odrOrderService.commit(request);
        log.info("订单提交返回报文：{}", RespVO.ok());
        return RespVO.okData(orderId);
    }

    @ApiOperation(value = "订单状态流转接口", notes = "订单状态流转接口", position = 2)
    @PostMapping(value = PathConstants.ODR_ORDRE_STATUS_UPDATE,consumes = MediaType.APPLICATION_JSON_VALUE,produces = MediaType.APPLICATION_JSON_VALUE)
    public RespVO statusUpdate(/*@ApiParam(value = "业务流水号", required = true, name = "x-b3-traceId") @RequestHeader("x-b3-traceId") String traceId,
                               @ApiParam(value = "请求流水号", required = true, name = "x-b3-spanId") @RequestHeader("x-b3-spanId") String spanId,*/
                               @RequestBody @ApiParam(value = "请求报文", required = true) @Valid OrderStatusUpdateReqVO request) {
        log.info("订单状态流转接口请求报文：{}",request);
        odrOrderService.statusUpdate(request);
        log.info("订单状态流转接口返回报文：{}", RespVO.ok());
        return RespVO.ok();
    }

    @ApiOperation(value = "订单详细信息查询", notes = "订单详细信息查询", position = 3)
    @PostMapping(value = PathConstants.ODR_ORDRE_DETAILS_QUERY,consumes = MediaType.APPLICATION_JSON_VALUE,produces = MediaType.APPLICATION_JSON_VALUE)
    public ResposeVO<OdrOrder> queryOdrOrderDetails(/*@ApiParam(value = "业务流水号", required = true,name = "x-b3-traceId") @RequestHeader("x-b3-traceId") String traceId,
                                                    @ApiParam(value = "请求流水号", required = true,name = "x-b3-spanId") @RequestHeader("x-b3-spanId") String spanId,*/
                                                    @RequestBody @ApiParam(value = "订单详情查询请求报文", required = true) @Valid QueryOdrOrderDetailsReqVO queryOdrOrderDetailsReqVO) {
        log.info("订单详细信息查询请求报文 queryOdrOrderDetailsReqVO={}]：", JSON.toJSONString(queryOdrOrderDetailsReqVO));
        ResposeVO<OdrOrder> resposeVO = odrOrderService.queryOdrOrderDetails(queryOdrOrderDetailsReqVO);
        log.info("订单详细信息查询响应报文[resposeVO={}]：", JSON.toJSONString(resposeVO));
        return resposeVO;
    }

    @ApiOperation(value = "订单列表查询", notes = "订单列表查询", position = 4)
    @PostMapping(value = PathConstants.ODR_ORDRE_LIST_QUERY,consumes = MediaType.APPLICATION_JSON_VALUE,produces = MediaType.APPLICATION_JSON_VALUE)
    public ResposeListVO<OdrOrder> queryOdrOrderList(/*@ApiParam(value = "业务流水号", required = true,name = "x-b3-traceId") @RequestHeader("x-b3-traceId") String traceId,
                                                               @ApiParam(value = "请求流水号", required = true,name = "x-b3-spanId") @RequestHeader("x-b3-spanId") String spanId,*/
                                                               @RequestBody @ApiParam(value = "请求报文", required = true) @Valid QueryOdrOrderReqVO queryOdrOrderReqVO) {
        log.info("订单列表信息查询请求报文[queryOrderReqVO={}]：", JSON.toJSONString(queryOdrOrderReqVO));
        ResposeListVO<OdrOrder> resposeVO = odrOrderService.queryOdrOrderList(queryOdrOrderReqVO);
        log.info("订单列表信息查询响应报文[resposeVO={}]：", JSON.toJSONString(resposeVO));
        return resposeVO;
    }
    @ApiOperation(value = "订单操作日志查询", notes = "订单操作日志查询", position = 5)
    @GetMapping(value = PathConstants.ODR_ORDRE_LOG_QUERY)
    public ResposeListVO<OdrOrderFlowLog> queryOdrOrderLog(@RequestParam(value = "orderId", required = true) String orderId,
                                                           @ApiParam(value = "000-待支付、001-已取消、002-待服务、003-服务中、004-已完成、005-已退款") @RequestParam(value = "preStatus", required = false) String preStatus,
                                                           @ApiParam(value = "000-待支付、001-已取消、002-待服务、003-服务中、004-已完成、005-已退款")@RequestParam(value = "targetStatus", required = false) String targetStatus,
                                                           @ApiParam(value = "用户id")@RequestParam(value = "userId", required = false) String userId,
                                                           @ApiParam(value = "用户名")@RequestParam(value = "userName", required = false) String userName,
                                                           @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
                                                           @RequestParam(value = "pageSize", defaultValue = "5") Integer pageSize) {
        ResposeListVO<OdrOrderFlowLog> odrOrderFlowLogResposeListVO = odrOrderService.queryOdrOrderLog(orderId, userId,userName, preStatus, targetStatus, pageNum, pageSize);
        return odrOrderFlowLogResposeListVO;
    }
}
